const express = require('express');
const router = express.Router();
const { get, post } = require('../util/http');
const { checkIp } = require('./../util/common');

// 获取dns
router.all('/GetDns', (req, res) => {
    post('GetDns').then(response => {
        res.send(response);
    }).catch((e) => {
        res.send(e)
    })
})

// 设置dns
router.all('/SetDns', (req, res) => {
    let params = req.body;
    if (typeof params.dns1 == 'undefined' || !checkIp(params.dns1)) {
        res.json({ status: false, msg: '主要DNS地址错误' });
        return;
    }
    if (typeof params.dns2 == 'undefined' || !checkIp(params.dns2)) {
        res.json({ status: false, msg: '备用DNS地址错误' });
        return;
    }
    post('SetDns', {
        dns1: params.dns1,
        dns2: params.dns2
    }).then(response => {
        res.send(response);
    }).catch((e) => {
        res.send(e)
    })
})

// 测试dns
router.all('/TestDns', (req, res) => {
    let params = req.body;
    if (typeof params.dns1 == 'undefined' || !checkIp(params.dns1)) {
        res.json({ status: false, msg: '主要DNS地址错误' });
        return;
    }
    if (typeof params.dns2 == 'undefined' || !checkIp(params.dns2)) {
        res.json({ status: false, msg: '备用DNS地址错误' });
        return;
    }
    post('TestDns', {
        dns1: params.dns1,
        dns2: params.dns2
    }).then(response => {
        res.send(response);
    }).catch((e) => {
        res.send(e)
    })
})

// 获取虚拟内存
router.all('/GetSwap', (req, res) => {
    post('GetSwap').then(response => {
        res.send(response);
    }).catch((e) => {
        res.send(e)
    })
})

// 设置虚拟内存
router.all('/SetSwap', (req, res) => {
    let params = req.body;
    if (typeof params.size == 'undefined' || isNaN(params.size) || params.size < 0) {
        res.json({ status: false, msg: 'size参数错误' });
        return;
    }
    post('SetSwap', { size: params.size }).then(response => {
        res.send(response);
    }).catch((e) => {
        res.send(e)
    })
})

// 获取时区
router.all('/GetZone', (req, res) => {
    let params = req.body;
    post('GetZone', {
        zone: typeof params.zone != 'undefined' && params.zone !== '' ? params.zone : 'Asia'
    }).then(response => {
        res.send(response);
    }).catch((e) => {
        res.send(e)
    })
})

// 设置时区
router.all('/SetZone', (req, res) => {
    let params = req.body;
    post('SetZone', {
        zone: typeof params.zone != 'undefined' && params.zone !== '' ? params.zone : 'Asia',
        area: typeof params.area != 'undefined' && params.area !== '' ? params.area : 'Shanghai',
    }).then(response => {
        res.send(response);
    }).catch((e) => {
        res.send(e)
    })
})

module.exports = router;